 <template>
   <view style="width: 100%;height: 100%;">
     <view class="root" :style="{display: !visibleCamera ? 'flex': 'none'}">
       <scroll-view class="content" scroll-y :scroll-top="scrollTop" @scroll="onScroll">
         <uni-forms ref="formRef" :model="form" validateTrigger="blur" label-position="top" label-width="100%"
           err-show-type="toast">
           <view class="content-wrapper">
             <yy-title title="维修电动车/摩托车信息"></yy-title>
             <view class="content-inner-wrapper">
               <uni-forms-item label="是否有车架编号：" name="hasFrameNumber" required key="hasFrameNumber">
                 <view class="form-content-item" :class="form.hasFrameNumber ?  '' : 'input-item'"
                   @click="hadleShowPicker('flag_type', 'hasFrameNumber')">
                   <uni-easyinput type="text" :maxlength="30" v-model.trim="form.hasFrameNumber_text"
                     :inputBorder="false" placeholder="请填写" :clearable="true" disabled />
                   <uni-icons type="right" size="21" color="#C2C0C0"></uni-icons>
                 </view>
               </uni-forms-item>
               <uni-forms-item label="车架编号：" name="frameNumber" required key="frameNumber"
                 v-if="form.hasFrameNumber == '1'">
                 <view class="form-content-item" :class="form.frameNumber ?  '' : 'input-item'">
                   <uni-easyinput type="text" :maxlength="30" v-model.trim="form.frameNumber" :inputBorder="false"
                     placeholder="请填写" :clearable="true" />
                 </view>
               </uni-forms-item>
               <uni-forms-item label="是否有车牌号：" name="hasCarNumber" required key="hasCarNumber">
                 <view class="form-content-item" :class="form.hasCarNumber ?  '' : 'input-item'"
                   @click="hadleShowPicker('flag_type', 'hasCarNumber')">
                   <uni-easyinput type="text" :maxlength="30" v-model.trim="form.hasCarNumber_text" :inputBorder="false"
                     placeholder="请选择" :clearable="true" disabled />
                   <uni-icons type="right" size="21" color="#C2C0C0"></uni-icons>
                 </view>
               </uni-forms-item>
               <uni-forms-item label="车牌号：" name="carNumber" required key="carNumber" v-if="form.hasCarNumber == '1'">
                 <view id="carNumberContainer" class="form-content-item" :class="form.carNumber ?  '' : 'input-item'"
                   @click="handleShowCar">
                   <uni-easyinput type="text" disabled v-model.trim="form.carNumber" :inputBorder="false"
                     placeholder="请选择" :clearable="true" />
                   <uni-icons type="right" size="21" color="#C2C0C0"></uni-icons>
                 </view>
               </uni-forms-item>
               <uni-forms-item label="品牌型号：" name="carModule" key="carModule">
                 <view class="form-content-item" :class="form.carModule ?  '' : 'input-item'">
                   <uni-easyinput type="text" :maxlength="30" v-model.trim="form.carModule" :inputBorder="false"
                     placeholder="请填写" :clearable="true" />
                 </view>
               </uni-forms-item>
               <uni-forms-item label="车辆图片:(最多可上传3张图片，非必填)" name="imageListUrl" style="margin-bottom: 0;">
                 <u-upload :fileList="form.imageListUrl" @afterRead="(info) => afterReadFive(info, 'form', 69)"
                   @delete="(info) => deletePhoto(info, 'form')" name="imageListUrl" :maxCount="3" :multiple="true"
                   width="96" height="80">
                   <image :src="baseImgUrl + 'upload-icon3.png'" style="width: 200rpx; height: 160rpx"></image>
                 </u-upload>
               </uni-forms-item>
               <uni-forms-item label="送修时间:" name="sendRepairTime" required key="sendRepairTime">
                 <view class="form-content-item" :class="form.sendRepairTime ?  '' : 'input-item'">
                   <uni-datetime-picker v-model="form.sendRepairTime" type="datetime" :hide-second="true"
                     :start="startTime" :end="endTime">
                     <uni-easyinput type="text" v-model="form.sendRepairTime" :inputBorder="false" placeholder="请选择"
                       :clearable="true" disabled />
                   </uni-datetime-picker>
                   <image :src="baseImgUrl + 'day-icon2.png'" style="width: 30rpx;height: 30rpx;padding:0 10rpx;">
                 </view>
               </uni-forms-item>
             </view>
           </view>
           <view class="content-wrapper">
             <yy-title title="送修人员信息"></yy-title>
             <uni-forms-item label="身份证号:" name="sendRepairIdcard" required key="sendRepairIdcard">
               <view class="form-content-item" :class="form.sendRepairIdcard ?  '' : 'input-item'">
                 <uni-easyinput type="idcard" :maxlength="18" v-model.trim="form.sendRepairIdcard" :inputBorder="false"
                   placeholder="请填写" :clearable="true" />
                 <u-icon slot="right" size="21" name="scan" color="#2488F5" @click="handleCamera"></u-icon>
               </view>
             </uni-forms-item>
             <uni-forms-item label="姓名:" name="sendRepairName" key="sendRepairName">
               <view class="form-content-item" :class="form.sendRepairName ?  '' : 'input-item'">
                 <uni-easyinput type="text" :maxlength="30" v-model.trim="form.sendRepairName" :inputBorder="false"
                   placeholder="请填写" :clearable="true" />
               </view>
             </uni-forms-item>
             <uni-forms-item label="联系电话:" name="sendRepairPhone" key="sendRepairPhone">
               <view class="form-content-item" :class="form.sendRepairPhone ?  '' : 'input-item'">
                 <uni-easyinput type="number" :maxlength="11" v-model.trim="form.sendRepairPhone" :inputBorder="false"
                   placeholder="请填写" :clearable="true" />
               </view>
             </uni-forms-item>
           </view>
         </uni-forms>
       </scroll-view>
       <view class="bottom">
         <yy-button-submit @submit="onSubmit"></yy-button-submit>
       </view>
     </view>
     <u-keyboard @backspace="backspace" autoChange safeAreaInsetBottom @cancel="hideKeyboard" @confirm="hideKeyboard"
       ref="uKeyboard" mode="car" :show="showKeyBoard" @change="valChange"></u-keyboard>
     <yy-photo-mask :visible.sync="visibleCamera" :baseImgUrl="baseImgUrl" :clientId="clientId"
       :clientSecret="clientSecret" @parsedSuccess="onParsedSuccess"></yy-photo-mask>
     <u-picker closeOnClickOverlay v-if="pickerShow" :show="pickerShow" :columns="pickerData" keyName="text"
       @confirm="onPickerConfirm" @cancel="pickerShow = false" @close="pickerShow = false"
       :defaultIndex="defaultIndex"></u-picker>
   </view>
 </template>

 <script>
   import {
     maintenanceRecordsAdd
   } from '../../../api/myself'
   import {
     electricvehiclesSave
   } from '@/api/api'
   import uploadFileMixin from '@/utils/upload-file-mixin'
   import {
     Toast,
     getOptionsParams,
     idCardRule,
     isLoading,
     navBack,
     phoneRule,
     getEndOfTodayTimestamp,
     getTimestampDaysAgo
   } from '@/utils/util'
   import moment from '@/utils/moment.js'
   import pickerMixin from '../../../utils/picker-mixin'

   export default {
     mixins: [uploadFileMixin, pickerMixin],
     data() {
       return {
         baseImgUrl: this.$baseImgUrl,
         routerParams: {},
         clientId: this.$clientId,
         clientSecret: this.$clientSecret,
         visibleCamera: false,
         form: this.initForm(),
         showKeyBoard: false,
         rules: {
           hasFrameNumber: {
             rules: [{
               required: true,
               errorMessage: "请选项是否有车架编号！",
             }]
           },
           frameNumber: {
             rules: [{
               required: true,
               errorMessage: "请填写车架编号！",
             }, {
               pattern: '^[0-9A-Z]+$',
               errorMessage: "车架编号只能输入数字和大写字母！"
             }]
           },
           hanCarNumber: {
             rules: [{
               required: false,
               errorMessage: "请选择是否有车牌号！",
             }]
           },
           carNumber: {
             rules: [{
               required: true,
               errorMessage: "请填写车牌号！",
             }]
           },
           carModule: {
             rules: [{
               required: false,
               errorMessage: "请填写品牌型号！",
             }]
           },
           sendRepairTime: {
             rules: [{
               required: true,
               errorMessage: "请选择送修时间！",
             }]
           },
           imageListUrl: {
             rules: [{
               required: false,
               errorMessage: "请上传电动车图片！",
             }]
           },
           sendRepairIdcard: {
             rules: idCardRule()
           },
           sendRepairName: {
             rules: [{
               required: false,
               errorMessage: "请填写姓名！",
             }]
           },
           sendRepairPhone: {
             rules: phoneRule(false)
           },
         },
         uKeyboard: null,
         startTime: getTimestampDaysAgo(5),
         endTime: getEndOfTodayTimestamp(),
         scrollTop: 0,
         windowWidth: 0,
         windowHeight: 0,
         carNumberRef: null
       }
     },
     onLoad(options) {
       this.routerParams = getOptionsParams(options)
       //  进入页面的时候执行一次
       uni.getSystemInfo({
         success: (res) => {
           const windowWidth = res.windowWidth; // 窗口宽度
           const windowHeight = res.windowHeight; // 窗口高度
           this.windowWidth = windowWidth
           this.windowHeight = windowHeight
         }
       });

     },
     onReady() {
       this.$refs.formRef.setRules(this.rules)
     },
     methods: {
       moment,
       getEndOfTodayTimestamp,
       initForm() {
         return {
           frameNumber: '',
           carNumber: '', // 车牌号
           carModule: '',
           sendRepairTime: '',
           sendRepairIdcard: '',
           sendRepairName: '',
           sendRepairPhone: '',
           imageListUrl: [],
           hasCarNumber: true,
           hasCarNumber_text: '是',
           hasFrameNumber: true,
           hasFrameNumber_text: '是'
         }
       },
       onSubmit() {
         this.$refs.formRef.validate().then(() => {
             let data = {
               companyMessageId: this.routerParams.companyMessageId,
               ...this.form,
               sendRepairTime: this.form.sendRepairTime ? moment(this.form
                 .sendRepairTime).format('YYYY/MM/DD HH:mm:ss') : '',
               carType: 2
             };

             // 处理图片信息
             if (data.imageListUrl) {
               data.imageListUrl = data.imageListUrl.map(item => {
                 return item.halfPath
               })
             }
             isLoading(true)
             maintenanceRecordsAdd(data).then((res) => {
                 if (res.code == 200) {
                   Toast(res.message);
                   uni.$emit('add-vehicle-maintenance-success')
                   setTimeout(() => {
                     navBack();
                   }, 100)
                 }
                 Toast(res.message);
               })
               .finally(() => {
                 isLoading(false)
               })
           })
           .catch(() => {})
       },
       handleCamera() {
         this.visibleCamera = true;
       },
       onParsedSuccess(value) {
         const {
           domicileAddress,
           userName,
           idcard,
           nation
         } = value;
         this.$set(this.form, "sendRepairName", userName);
         this.$set(this.form, "sendRepairIdcard", idcard);
         this.visibleCamera = false;
       },
       handleShowCar() {
         this.showKeyBoard = true
         this.$nextTick(() => {
           uni.createSelectorQuery().in(this.$refs.uKeyboard)
             .select('.u-keyboard')
             .boundingClientRect((res) => {
               let keyboardHeight = res.height
               // 获取元素底部距离顶部的距离 - 车牌号弹窗距离顶部的距离
               let keyboardTop = this.windowHeight - keyboardHeight
               // 在获取一下输入框的位置

               const query = uni.createSelectorQuery();
               // 选择页面中id为myElement的元素，并获取其位置信息
               query.select('#carNumberContainer').boundingClientRect((rect) => {
                 // rect.top就是元素距离页面顶部的距离
                 let rectTop = rect.top + rect.height + this.scrollTop

                 let scrollTopValue = rectTop - keyboardTop
                 // 如果滚动的距离 - 元素距离顶部的距离大于  键盘顶部距离上面的距离，则设置 
                 this.scrollTop = scrollTopValue
                 // 你可以在这里对高度值进行进一步的处理或返回
               }).exec(); // 执行查询

             })
             .exec();
         })
       },
       // 按键被点击(点击退格键不会触发此事件)
       valChange(val) {
         // 将每次按键的值拼接到value变量中，注意+=写法
         if (this.form.carNumber.length >= 15) {
           Toast('车牌号最多输入15位！')
           return
         }
         this.form.carNumber += val;
         console.log(this.form.carNumber);
       },
       // 退格键被点击
       backspace() {
         // 删除value的最后一个字符
         if (this.form.carNumber.length) {
           this.form.carNumber = this.form.carNumber.substr(0, this.form
             .carNumber.length - 1)
           if (this.form.carNumber.length === 0) {
             // 没有 length 的时候
             // 切换成中文
             this.$refs.uKeyboard.$children[0].$children[2].abc = false
           }
         }
       },
       hideKeyboard() {
         this.showKeyBoard = false
         this.scrollTop = 0
       },
       onScroll(event) {
         const {
           scrollTop
         } = event.detail
         this.scrollTop = scrollTop
       }
     }
   }
 </script>

 <style lang="scss" scoped>
   @import '@/styles/uni-form-label-top.scss';

   /deep/.u-keyboard__button--center {
     .u-keyboard__button__inner-wrapper:first-of-type {
       display: none;
     }
   }

   .root {
     display: flex;
     width: 100%;
     height: 100%;
     flex-direction: column;
     background: #F9F9F9;

     .content {
       flex: 1;
       height: 0;
       overflow: auto;

       .content-wrapper {
         background: #FFFFFF;
         box-shadow: 0rpx 3rpx 6rpx 1rpx rgba(207, 207, 207, 0.1608);
         border-radius: 8rpx 8rpx 8rpx 8rpx;
         padding: 40rpx 20rpx;
         margin-bottom: 10rpx;
       }

       .pawn-list-wrapper {
         background: #F9F9F9;
         padding: 25rpx;
         margin-bottom: 10rpx;

         .title-name {
           display: flex;
           justify-content: space-between;
           margin-bottom: 23rpx;

           .name-i {
             font-weight: bold;
             font-size: 32rpx;
             color: #181818;
           }

           .delete-i {
             display: flex;
             align-items: center;
             font-weight: 500;
             font-size: 26rpx;
             color: #F94A4A;
             flex-shrink: 0;

             .text-delete {
               margin-left: 10rpx;
             }
           }
         }

         .i-item {
           margin-bottom: 15rpx;
           display: flex;

           .i-label {
             font-weight: 500;
             font-size: 28rpx;
             color: #999999;
             width: 119rpx;
             margin-right: 16rpx;
             position: relative;
             text-align: justify;
             text-align-last: justify;
             flex-shrink: 0;



             &::after {
               content: ':';
               position: absolute;
               right: -8rpx;
             }
           }

           .i-value {
             font-weight: bold;
             font-size: 28rpx;
             color: #101010;
           }

         }
       }

       .datetime-picker-wrapper {
         display: flex;
         align-items: center;
         justify-content: space-between;

         .text-t {
           font-weight: 500;
           font-size: 30rpx;
           color: #666666;
         }

         .t-item {
           font-weight: 400;
           font-size: 28rpx;
           color: #A2A2A2;
           width: 304rpx;
           height: 70rpx;
           background: #F9F9F9;
           border-radius: 8rpx 8rpx 8rpx 8rpx;
           border: 1rpx solid #F1EEEE;
           display: flex;
           align-items: center;
           justify-content: center;
         }

         .has-value {
           font-weight: bold;
           font-size: 30rpx;
           color: #181818;
         }
       }
     }
   }
 </style>